﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Collections.Generic;
using System.Drawing;
using IronOne.SalesOne.BusinessObjects;


namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class SearchMessages :SOSecurityPage, IDataPage,IntTest
    {
        Facade.StructureFacade structureFacade = new Facade.StructureFacade();
        Facade.MessageFacade messageFacade = new Facade.MessageFacade();
        static List<Branch> lstBranches = new List<Branch>();
        static List<SalesOneUnit> lstUnits = new List<SalesOneUnit>();
        static List<string> lstSocodes = new List<string>();

        private static string userName ;
        private static string userId;
        private static string unitValue;
        //static UserInformation info;

        private static int userType;
        private static int groupId;

        private static int pageNo = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //if (Session["firstRow"] != null)
                //{
                //    Session["messgeId"] = ((GridViewRow)Session["firstRow"]).Cells[7].Text;
                //    Session.Remove("firstRow");
                //}
                


                UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                              
                    Button btnCreate = (Button)UCGridButtons1.FindControl("btnCreate");
                    btnCreate.Visible = false;
                    System.Web.UI.HtmlControls.HtmlTableCell TestCell = (System.Web.UI.HtmlControls.HtmlTableCell)UCGridButtons1.FindControl("tdCreateButton");
                    TestCell.Visible = false;

                    //Button btnCreate = (Button)this.FindControl("btnCreate");            

                if (!IsPostBack)
                {
                    InsertDDLItems(ddlBranch, "Select...", 0);
                    InsertDDLItems(ddlUnit, "Select...", 0);
                    InsertDDLItems(ddlSOCode, "Select...", 0);

                    MembershipUser membershipUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                    userName = membershipUser.UserName;

                    //SetDataSourceToFrom(structureFacade.GetUserTypesForUser("UserTypeId ASC", userName));
                    SetDataSourceToFrom(structureFacade.GetUserTypes(""));
                    //ddlFrom.SelectedValue = (structureFacade.GetUserTypeForUser(userName).UserTypeId.ToString());
                    ddlFrom.SelectedIndex = 0;
                    SetControlsForUser(userName);

                    UserInformation info = structureFacade.GetUserInformation(User.Identity.Name);
                    userType = info.UserType;

                    if (userType != 1)
                        groupId = (int)info.GroupId;
                    else
                        groupId = 0;

                    if (Page.Request.Params["MessageId"] != null)
                    {
                        int messageId = Convert.ToInt16(Page.Request.Params["MessageId"].ToString());
                        ddlFrom.SelectedIndex = userType;

                        if (userType == 1)
                            chkBroadcast.Enabled = true;

                        UCGrid1.SetText("1");
                        Search(1, "Upadated DSC");


                        GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

                        foreach (GridViewRow row in gr.Rows)
                        {
                            if (row.Cells[7].Text.Equals(messageId.ToString()))
                            {
                                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                                rbtn.Checked = true;
                                Session["messgeId"] = messageId.ToString();

                            }
                            else
                            {
                                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                                rbtn.Checked = false;
                            }

                        }
                    }
                    else
                    {

                    }

                }
                //lblError.ForeColor = Color.Black;
                //lblError.Text = "";
            }
            catch { MassageSummery("IIS514"); }
        }

        public void SetDataSourceToBranches(List<Branch> branches)
        {
           
            try
            {
                ddlBranch.SelectedIndex = 0;
            }
            catch { }
            
            //catch { ddlBranch.SelectedIndex = -1; }
            //ddlBranch.Items.Clear();

            if (branches.Count > 0)
            {
                ddlBranch.DataSource = branches;
                ddlBranch.DataTextField = "BranchName";
                ddlBranch.DataValueField = "BranchID";
                ddlBranch.DataBind();
            }

            InsertDDLItems(ddlBranch, "Select...", 0);
            InsertDDLItems(ddlBranch, "All", 1);
        }

        public void SetDataSourceToUnits(List<SalesOneUnit> units)
        {
            try
            {
                ddlUnit.SelectedIndex = 0;
            }
            catch { }
            
            //catch { ddlUnit.SelectedIndex = -1; }
            //ddlUnit.Items.Clear();

            if (units.Count > 0)
            {
                ddlUnit.DataSource = units;
                ddlUnit.DataTextField = "Name";
                ddlUnit.DataValueField = "UnitId";
                ddlUnit.DataBind();
            }

            InsertDDLItems(ddlUnit, "Select...", 0);
            InsertDDLItems(ddlUnit, "All", 1);
            //ddlUnit.SelectedIndex = 0;
        }

        public void SetDataSourceToSoCode(List<string> users)
        {
            try
            {
                ddlSOCode.SelectedIndex = 0;
            }
            catch { }
            //catch { ddlSOCode.SelectedIndex = -1; }
            //ddlSOCode.Items.Clear();

            if (users.Count > 0)
            {
                ddlSOCode.DataSource = users;
                ddlSOCode.DataBind();
            }

            InsertDDLItems(ddlSOCode, "Select...", 0);
            InsertDDLItems(ddlSOCode, "All", 1);
            ddlSOCode.SelectedIndex = 0;
        }

        /*public void SetDataSourceToSoCode(List<User> users)
        {
            ddlSOCode.DataSource = users;
            ddlSOCode.DataTextField = "Name";
            ddlSOCode.DataValueField = "UserId";
            ddlSOCode.DataBind();

            InsertDDLItems(ddlSOCode, "Select...", 0);
            InsertDDLItems(ddlSOCode, "All", 1);
        }
        */

        public void SetDataSourceToFrom(List<UserType> userTypes)
        {
            try
            {
                ddlBranch.SelectedIndex = 0;
            }
            catch { }
            //catch { ddlBranch.SelectedIndex = -1; }
            //ddlBranch.Items.Clear();

            if (userTypes.Count>0)
            {
                ddlFrom.DataSource = userTypes;
                ddlFrom.DataTextField = "UserTypeName";
                ddlFrom.DataValueField = "UserTypeId";
                ddlFrom.DataBind();
            }

            InsertDDLItems(ddlFrom, "Select...", 0);
            //InsertDDLItems(ddlFrom, "All", 1);
        }

        private void SetControlsForUser(string  userName,bool status)
        {
            User user = structureFacade.GetUser(userName);

            userId = user.UserId.ToString();

            UserInformation info = structureFacade.GetUserInformation(User.Identity.Name);
            userType = info.UserType;
         
            /*Sajith Code using sp*/
            bool flag = false;


            if (userType == 4)
            {
                //if (user.SoCode != null)
                //{
                //    lstSocodes = new List<string>();
                //    lstSocodes.Add(user.SoCode);
                //    SetDataSourceToSoCode(lstSocodes);
                //    ddlSOCode.SelectedIndex = ddlSOCode.Items.Count - 1;
                //    ddlSOCode.Enabled = false;
                //}
                //else
                //{
                    SetDataSourceToSoCode(structureFacade.GetSOsInUnit(user.NodeId));
                if(ddlSOCode.SelectedIndex==-1)
                    ddlSOCode.SelectedIndex = 0;
                //}
                flag = true;
            }

            if (flag == true)
            {
                lstUnits = new List<SalesOneUnit>();
                lstUnits.Add(structureFacade.GetUnit(Convert.ToInt32(info.UnitId)));
                SetDataSourceToUnits(lstUnits);
                ddlUnit.SelectedIndex = ddlUnit.Items.Count - 1;
                ddlUnit.Enabled = false;
            }
            else if (userType == 3)
            {
                lstUnits = structureFacade.GetUnitsForBranch(Convert.ToInt32(user.NodeId));
                SetDataSourceToUnits(lstUnits);
                
                if (!IsPostBack)
                    ddlUnit.SelectedIndex = 0;
                ddlBranch.SelectedIndex = ddlBranch.Items.Count - 1;
                flag = true;
            }

            if (flag == true)
            {
                lstBranches = new List<Branch>();
                lstBranches.Add(structureFacade.GetBranch(Convert.ToInt32(info.BranchId)));
                SetDataSourceToBranches(lstBranches);
                ddlBranch.SelectedIndex = ddlBranch.Items.Count - 1;

                ddlBranch.Enabled = false;
            }
            //else if (userType == 2)
            //{
            //    if (status)
            //    {
            //        lstBranches = structureFacade.GetBranchesForGroup(Convert.ToInt32(user.NodeId));
            //        SetDataSourceToBranches(lstBranches);
            //        ddlBranch.SelectedIndex = 0;
            //        flag = true;
            //    }
            //}

            //if (userType == 1)
            //{
            //    if (status)
            //    {
            //        chkBroadcast.Enabled = true;
            //        lstBranches = structureFacade.GetBranches("BranchName ASC");
            //        SetDataSourceToBranches(lstBranches);
            //        ddlBranch.SelectedIndex = 0;
            //    }
            //}
           
        }

        private void SetControlsForUser(string userName)
        {
            User user = structureFacade.GetUser(userName);

            userId = user.UserId.ToString();

            UserInformation info = structureFacade.GetUserInformation(User.Identity.Name);
            userType = info.UserType;

            /*Sajith Code using sp*/
            bool flag = false;


            if (userType == 4)
            {
                //if (user.SoCode != null)
                //{
                //    lstSocodes = new List<string>();
                //    lstSocodes.Add(user.SoCode);
                //    SetDataSourceToSoCode(lstSocodes);
                //    ddlSOCode.SelectedIndex = ddlSOCode.Items.Count - 1;
                //    ddlSOCode.Enabled = false;
                //}
                //else
                //{
                SetDataSourceToSoCode(structureFacade.GetSOsInUnit(user.NodeId));
                //if (ddlSOCode.SelectedIndex == -1)
                    ddlSOCode.SelectedIndex = 0;
                //}
                flag = true;
            }

            if (flag == true)
            {
                lstUnits = new List<SalesOneUnit>();
                lstUnits.Add(structureFacade.GetUnit(Convert.ToInt32(info.UnitId)));
                SetDataSourceToUnits(lstUnits);
                ddlUnit.SelectedIndex = ddlUnit.Items.Count - 1;
                ddlUnit.Enabled = false;
            }
            else if (userType == 3)
            {

                lstUnits = structureFacade.GetUnitsForBranch(Convert.ToInt32(user.NodeId));
                SetDataSourceToUnits(lstUnits);
                if (!IsPostBack)
                    ddlUnit.SelectedIndex = 0;
                else
                {
                    ddlUnit.SelectedValue = unitValue;
                }

                ddlBranch.SelectedIndex = ddlBranch.Items.Count - 1;
                flag = true;
            }

            if (flag == true)
            {
                lstBranches = new List<Branch>();
                lstBranches.Add(structureFacade.GetBranch(Convert.ToInt32(info.BranchId)));
                SetDataSourceToBranches(lstBranches);

                ddlBranch.SelectedIndex = ddlBranch.Items.Count - 1;

                ddlBranch.Enabled = false;
            }
            //else if (userType == 2)
            //{
            //    if (status)
            //    {
            //        lstBranches = structureFacade.GetBranchesForGroup(Convert.ToInt32(user.NodeId));
            //        SetDataSourceToBranches(lstBranches);
            //        ddlBranch.SelectedIndex = 0;
            //        flag = true;
            //    }
            //}

            //if (userType == 1)
            //{
            //    if (status)
            //    {
            //        chkBroadcast.Enabled = true;
            //        lstBranches = structureFacade.GetBranches("BranchName ASC");
            //        SetDataSourceToBranches(lstBranches);
            //        ddlBranch.SelectedIndex = 0;
            //    }
            //}

        }

        public void InsertDDLItems(DropDownList ddl, string Text, int Position)
        {
            ListItem item = new ListItem(Text,Text);
            ddl.Items.Insert(Position, Text);
            //ddl.Items.Add( item);
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                UCGrid1.SetText("1");
                Search(1, "MessageId ASC");              
            }
            catch { MassageSummery("IIS514"); }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                Create();
            }
            catch { MassageSummery("IIS514"); }
        }


        protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            try{
            if (ddlFrom.SelectedIndex > 0)
            {
                EnableControls(true);
                if (ddlBranch.SelectedIndex > 1)
                {
                    lstUnits = structureFacade.GetUnitsInBranch(int.Parse(ddlBranch.SelectedValue));
                    SetDataSourceToUnits(lstUnits);
                    ddlSOCode.Items.Clear();
                    InsertDDLItems(ddlSOCode, "Select...", 0);
                    ddlUnit.SelectedIndex = 0;
                }
                else if (ddlBranch.SelectedIndex == 1)
                {
                    ddlUnit.Enabled = false;
                    ddlUnit.Items.Clear();
                    ddlSOCode.Enabled = false;
                    ddlSOCode.Items.Clear();
                    InsertDDLItems(ddlSOCode, "Select...", 0);
                }
                else
                {
                    ClearControls(1);
                }
                SetControlsForUser(userName);
            }
            else
            {
                MassageSummery("IIS505");

                if (ddlBranch.Items.Count > 0)
                    ddlBranch.SelectedIndex = 0;
            }
            }
            catch { MassageSummery("IIS514"); }
        }

        protected void chkBroadcast_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                DisableControlsForBroadcasting();
                if (!chkBroadcast.Checked)
                {
                    if (ddlFrom.SelectedIndex > 0)
                    {
                        if (userType == 2)
                        {
                            lstBranches = structureFacade.GetBranchesForGroup(groupId);
                            SetDataSourceToBranches(lstBranches);
                            ddlBranch.SelectedIndex = 0;
                        }
                        else
                        {
                            lstBranches = structureFacade.GetBranches("BranchName ASC");
                            SetDataSourceToBranches(lstBranches);
                            ddlBranch.SelectedIndex = 0;
                        }
                        if (ddlFrom.SelectedItem.Text.Equals("HQ", StringComparison.InvariantCultureIgnoreCase))
                        {
                            chkBroadcast.Enabled = true;
                            ClearControls(1);
                            EnableControls(true);
                        }
                        else
                        {
                            DisableControlsForHQ();
                            ClearControls(1);
                            EnableControls(true);
                        }
                        SetControlsForUser(userName);

                    }
                    else
                    {
                        ClearControls();
                    }
                }
            }
            catch { MassageSummery("IIS514"); }
        }

        protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlFrom.SelectedIndex > 0)
                {
                    unitValue = ddlUnit.SelectedValue;
                    EnableControls(true);
                    if (ddlUnit.SelectedIndex > 1)
                    {
                        lstSocodes = structureFacade.GetSOsInUnit(int.Parse(ddlUnit.SelectedValue));
                        SetDataSourceToSoCode(lstSocodes);
                        //ddlSOCode.Items.Clear();
                    }
                    else if (ddlUnit.SelectedIndex == 1)
                    {
                        ddlSOCode.Enabled = false;
                        ddlSOCode.Items.Clear();
                        InsertDDLItems(ddlSOCode, "Select...", 0);
                    }
                    else
                        ClearControls(2);
                    SetControlsForUser(userName);
                }
                else
                {
                    MassageSummery("IIS505");

                    if (ddlUnit.Items.Count > 0)
                        ddlUnit.SelectedIndex = 0;
                }
            }
            catch { MassageSummery("IIS514"); }
        }

        protected void ddlFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlFrom.SelectedIndex > 0)
                {
                    if (userType == 2)
                    {
                        lstBranches = structureFacade.GetBranchesForGroup(groupId);
                        SetDataSourceToBranches(lstBranches);
                        ddlBranch.SelectedIndex = 0;
                    }
                    else
                    {
                        lstBranches = structureFacade.GetBranches("BranchName ASC");
                        SetDataSourceToBranches(lstBranches);
                        ddlBranch.SelectedIndex = 0;
                    }
                    if (ddlFrom.SelectedItem.Text.Equals("HQ", StringComparison.InvariantCultureIgnoreCase))
                    {
                        chkBroadcast.Enabled = true;
                        ClearControls(1);
                        EnableControls(true);
                    }
                    else
                    {
                        DisableControlsForHQ();
                        ClearControls(1);
                        EnableControls(true);
                    }
                    SetControlsForUser(userName);
                    //bool status = true;
                    //if (ddlFrom.SelectedItem.Text.Equals("HQ", StringComparison.InvariantCultureIgnoreCase))
                    //{
                    //    chkBroadcast.Enabled = true;
                    //    lstBranches = structureFacade.GetBranches("BranchName ASC");
                    //    SetDataSourceToBranches(lstBranches);
                    //    ddlBranch.SelectedIndex = 0;
                    //    ClearControls(1);
                    //    EnableControls(true);
                    //}
                    ///*If Group selected populate the branches according to the group*/
                    //else if (ddlFrom.SelectedItem.Text.Equals("Group", StringComparison.InvariantCultureIgnoreCase))
                    //{
                    //    DisableControlsForHQ();
                    //    //populate branches
                    //    lstBranches = structureFacade.GetBranchesForGroup("BranchName ASC", userName);
                    //    SetDataSourceToBranches(lstBranches);
                    //    ClearControls(1);
                    //    EnableControls(true);
                    //}
                    ///*If brnch - populate the branches according to the selected branch */
                    //else if (ddlFrom.SelectedItem.Text.Equals("Branch", StringComparison.InvariantCultureIgnoreCase))
                    //{
                    //    DisableControlsForHQ();
                    //    //populate units
                    //    lstUnits = structureFacade.GetUnitsInBranch("UnitName", userName);
                    //    SetDataSourceToUnits(lstUnits);
                    //    ddlBranch.Enabled = false;
                    //    ClearControls(4);
                    //    status = false;
                    //}
                    ///*If units then -units acoording to the .....*/
                    //else if (ddlFrom.SelectedItem.Text.Equals("Unit", StringComparison.InvariantCultureIgnoreCase))
                    //{
                    //    lstSocodes = structureFacade.GetSOsForUser("", userName);
                    //    SetDataSourceToSoCode(lstSocodes);
                    //    DisableControlsForHQ();
                    //    ddlBranch.Enabled = false;                   
                    //    ddlUnit.Enabled = false;
                    //    ClearControls(3);
                    //    status = false;
                    //}
                    //SetControlsForUser(userName,status);
                }
                else
                {
                    ClearControls();
                }
            }
            catch { MassageSummery("IIS514"); }
        }


        private void DisableControlsForBroadcasting()
        {
            if (chkBroadcast.Checked)
            {
                EnableControls(false);
                ClearControls();
            }
            else
            {
                EnableControls(true);
                SetControlsForUser(userName,true);
            }           
        }

        private void DisableControlsForHQ()
        {
            chkBroadcast.Enabled = false;
            chkBroadcast.Checked = false;
            DisableControlsForBroadcasting();
        }

        private void EnableControls(bool enabled)
        {
            ddlBranch.Enabled = enabled;
            ddlUnit.Enabled = enabled;
            ddlSOCode.Enabled = enabled;
        }

        private void ClearControls(int status)
        {
            if (status == 0)
            {
                ddlBranch.Items.Clear();
                ddlUnit.Items.Clear();
                ddlSOCode.Items.Clear();

                InsertDDLItems(ddlBranch, "Select...", 0);
                InsertDDLItems(ddlUnit, "Select...", 0);
                InsertDDLItems(ddlSOCode, "Select...", 0);

            }else if(status==1)
            {
                ddlUnit.Items.Clear();
                ddlSOCode.Items.Clear();

                InsertDDLItems(ddlUnit, "Select...", 0);
                InsertDDLItems(ddlSOCode, "Select...", 0);
            }
            else if(status==2)
            {
                ddlSOCode.Items.Clear();
                InsertDDLItems(ddlSOCode, "Select...", 0);
            }
            else if(status==3)
            {
                ddlBranch.Items.Clear();
                ddlUnit.Items.Clear();

                InsertDDLItems(ddlBranch, "Select...", 0);
                InsertDDLItems(ddlUnit, "Select...", 0);
            }
            else if(status==4)
            {
                ddlBranch.Items.Clear();
                ddlSOCode.Items.Clear();

                InsertDDLItems(ddlBranch, "Select...", 0);
                InsertDDLItems(ddlSOCode, "Select...", 0);
            }
        }

        private void ClearControls()
        {
            ddlBranch.Items.Clear();
            ddlUnit.Items.Clear();
            ddlSOCode.Items.Clear();

            InsertDDLItems(ddlBranch, "Select...", 0);
            InsertDDLItems(ddlUnit, "Select...", 0);
            InsertDDLItems(ddlSOCode, "Select...", 0);
        }
        
        private bool GetBranch(Branch branch)
        {
            if (branch.BranchId == Convert.ToInt32(ddlBranch.SelectedValue))
                return true;
            else
                return false;

        }

        private bool GetUnit(SalesOneUnit unit)
        {
            if (unit.UnitId == Convert.ToInt32(ddlUnit.SelectedValue))
                return true;
            else
                return false;
        }

        private bool GetSOCode(string soCode)
        {
            if (soCode.Equals(ddlSOCode.SelectedItem.Text))
                return true;
            else
                return false;

        }

        public void Search(int page, string sortExpression)
        {
            try
            {
                if (ddlFrom.SelectedIndex > 0)
                {
                    SearchMessage searchMessage = new SearchMessage();
                    searchMessage.UserType = Convert.ToInt32(ddlFrom.SelectedValue);
                    searchMessage.Broadcasted = chkBroadcast.Checked;

                    if (ddlBranch.SelectedIndex > 0)
                    {
                        if (ddlBranch.SelectedIndex == 1)
                            searchMessage.Branches = lstBranches;
                        else
                        {
                            List<Branch> lst = new List<Branch>();
                            lst.Add(lstBranches.Find(GetBranch));
                            searchMessage.Branches = lst;
                        }

                    }
                    if (ddlUnit.SelectedIndex > 0)
                    {
                        if (ddlUnit.SelectedIndex == 1)
                            searchMessage.Units = lstUnits;
                        else
                        {
                            List<SalesOneUnit> lst = new List<SalesOneUnit>();
                            lst.Add(lstUnits.Find(GetUnit));
                            searchMessage.Units = lst;
                        }

                    }
                    if (ddlSOCode.SelectedIndex > 0)
                    {
                        if (ddlSOCode.SelectedIndex == 1)
                            searchMessage.SOCodes = lstSocodes;
                        else
                        {
                            List<string> lst = new List<string>();
                            lst.Add(ddlSOCode.SelectedItem.Text);
                            searchMessage.SOCodes = lst;
                        }
                    }
                    //int[] numbers = {};
                    int[] numbers = { 1, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
                    List<Message> messages = messageFacade.SearchMessages(searchMessage, page, sortExpression);
                    if (messages.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (messageFacade.SearchMessages(searchMessage, page + 1, sortExpression)).Count > 0 ? false : true;
                    }
                    
                    if (messages.Count > 0)
                    {
                        UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "UserTypename",
                    ColumnHeader = "From",
                    ColumnWidth = 50},
                new GridViewStyle{
                    ColumnId = "MessageType",
                    ColumnHeader = "Type",
                    ColumnWidth = 80},
                new GridViewStyle{
                    ColumnId = "BranchName",
                    ColumnHeader = "Branch Name",
                    ColumnWidth = 100},
                new GridViewStyle{
                    ColumnId = "SoCodeId",
                    ColumnHeader = "SO Code",
                    ColumnWidth = 100},
                    new GridViewStyle{
                    ColumnId = "MessageBody",
                    ColumnHeader = "Message",
                    ColumnWidth = 150}
            };

                        UCGrid1.SetDataSource(messages, numbers);
                        UCGridButtons1.Visible = true;
                        UCGrid1.Visible = true;
                        Button btnCreate = (Button)UCGridButtons1.FindControl("btnCreate");
                        //Button btnCreate = (Button)this.FindControl("btnCreate");
                        btnCreate.Visible = false;
                    }
                    else
                    {
                        UCGridButtons1.Visible = false;
                        UCGrid1.Visible = false;

                        MassageSummery("IIS504");
                    }

                }
                else
                {
                    MassageSummery("IIS505");
                }
            }
            catch { MassageSummery("IIS514"); }
        }

        #region IDataPage Members

        public void Create()
        {
            Response.Redirect("~/Messaging/CreateMessage.aspx");
        }

        public void View()
        {
            try
            {
                //if (Session["messgeId"] == null)
                //{

                //    Session["messgeId"] = ((GridViewRow)Session["firstRow"]).Cells[7].Text;
                //    Session.Remove("firstRow");
                //}
                GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

                foreach (GridViewRow row in gr.Rows)
                {
                    RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                    if(rbtn.Checked)
                    {
                        Response.Redirect("~/Messaging/ViewMessage.aspx?MessageId=" + row.Cells[7].Text.Trim() + "&a=v");
                    }
                }
                //Response.Redirect("~/Messaging/ViewMessage.aspx?MessageId=" + Session["messgeId"].ToString() + "&a=v");
            }
            catch { MassageSummery("IIS514"); }
        }

        public void Update()
        {
            try
            {
                //if (Session["messgeId"] == null)
                //{

                //    Session["messgeId"] = ((GridViewRow)Session["firstRow"]).Cells[7].Text;
                //    Session.Remove("firstRow");
                //}
                GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

                foreach (GridViewRow row in gr.Rows)
                {
                    RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                    if (rbtn.Checked)
                    {
                        Message message = messageFacade.GetMessage(Convert.ToInt32(row.Cells[7].Text.Trim()));
                        User user = structureFacade.GetUser(userName);
                        if (message.CreatedBy.ToString().Equals(user.UserId.ToString(), StringComparison.CurrentCultureIgnoreCase))
                            Response.Redirect("~/Messaging/UpdateMessage.aspx?MessageId=" + row.Cells[7].Text.Trim() + "&a=u");
                        else
                            MassageSummery("IIS513");
                        return;
                    }
                }
                //Response.Redirect("/Messaging/UpdateMessage.aspx?a=19");
            }
            catch { MassageSummery("IIS514"); }
        }

        public void Delete()
        {
            try
            {
                GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

                foreach (GridViewRow row in gr.Rows)
                {
                    RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                    if (rbtn.Checked)
                    {
                        Message message = messageFacade.GetMessage(Convert.ToInt32(row.Cells[7].Text.Trim()));
                        messageFacade.DeleteMessage(message);
                        return;
                    }
                }
            }
            catch { MassageSummery("IIS514"); }
        }

        public void Cancel()
        {

        }

        #endregion


        #region IDataPage Members


        public void GetId(GridViewRow row)
        {
            Session["messgeId"] = row.Cells[7].Text;
        }

        #endregion


        #region IIntTest Members


        public void Next()
        {
            try
            {
                if (UCGrid1.GetText() != "1")
                {
                    pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                    UCGrid1.SetText(pageNo.ToString());

                }
                else
                {
                    pageNo = 2;
                    UCGrid1.SetText("2");

                }

                Search(pageNo, "MessageId ASC");
            }
            catch { MassageSummery("IIS514"); }
        }

        public void Previous()
        {
            try
            {
                if (UCGrid1.GetText() != "1")
                {
                    pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                    UCGrid1.SetText(pageNo.ToString());

                    Search(pageNo, "MessageId ASC");
                }
            }
            catch { MassageSummery("IIS514"); }
        }

        public void Go()
        {
            try
            {
                int p = int.Parse(UCGrid1.GetText());
                if (p != 0)
                {
                    try
                    {
                        Search(p, "MessageId ASC");
                        pageNo = p;
                    }
                    catch (Exception)
                    {

                        throw;
                    }


                }
            }
            catch { MassageSummery("IIS505"); }

        }

        #endregion

        /*Appendix*/
        //searchMessage.Units=
        //searchMessage.SOCodes=

        //if (ddlFrom.SelectedIndex == 1)
        //    //UCGrid1.SetDataSource(messageFacade.GetMessages("MessageId ASC"));
        //    UCGrid1.SetDataSource(messageFacade.SearchMessages(5,true,1,1,"1212"));
        //else
        //    UCGrid1.SetDataSource(messageFacade.GetMessagesSentBy(Convert.ToInt32(ddlFrom.SelectedValue)));

        /*
        if (ddlSOCode.SelectedIndex > 0)
        {
            if(ddlSOCode.SelectedIndex==1)
                messageFacade.SearchMessages(Convert.ToInt32(ddlFrom.SelectedValue), false, -1, -1, "*");
            else
                messageFacade.SearchMessages(Convert.ToInt32(ddlFrom.SelectedValue), false, -1, -1, ddlSOCode.SelectedItem.Text);
        }
        else if (ddlUnit.SelectedIndex > 0)
        {
            if (ddlUnit.SelectedIndex == 1)
                messageFacade.SearchMessages(Convert.ToInt32(ddlFrom.SelectedValue), false, -1, 0, "");
            else
                messageFacade.SearchMessages(Convert.ToInt32(ddlFrom.SelectedValue), false, -1, Convert.ToInt32(ddlUnit.SelectedValue), "");
        }
        else if (ddlBranch.SelectedIndex > 0)
        {
            if (ddlBranch.SelectedIndex == 1)
                messageFacade.SearchMessages(Convert.ToInt32(ddlFrom.SelectedValue), false, 0, -1, "");
            else
                messageFacade.SearchMessages(Convert.ToInt32(ddlFrom.SelectedValue), false, Convert.ToInt32(ddlBranch.SelectedValue), -1, "");
        }
        else if (chkBroadcast.Checked)
        {
            messageFacade.SearchMessages(Convert.ToInt32(ddlFrom.SelectedValue), true, -1, -1, "");
        }
        else
            messageFacade.SearchMessages(Convert.ToInt32(ddlFrom.SelectedValue), false, -1, -1, "");

        //int userTypeID;
        //if(ddlFrom.SelectedValue)
        //{
        //    userTypeID= Convert.ToInt32(ddlFrom.SelectedValue));
         * */

        

    }
}
